#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex1= PTHREAD_MUTEX_INITIALIZER;
//pthread_mutex_t mutex2= PTHREAD_MUTEX_INITIALIZER;

int num=0;

void *myfunc(void *arg)
{
	int i=*(int *)arg;
	while(i)
	{
		pthread_mutex_lock(&mutex1);
		int a=num;
		a++;
		num=a;

		pthread_mutex_unlock(&mutex1);
		i--;
	}
}
int main(int argc, const char *argv[])
{
	pthread_t tid;
	int i=50;
	pthread_create(&tid,NULL,myfunc,&i);
//	pthread_detach(tid);
	int d=50;
	while(d)
	{
		pthread_mutex_lock(&mutex1);
		int a=num;
		a++;
		num=a;
		pthread_mutex_unlock(&mutex1);
		d--;
	}

	pthread_join(tid,NULL);
//	pthread_detach(tid);
	pthread_mutex_destroy(&mutex1);
	printf("num=%d\n",num);
	
	return 0;
}
